﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security.Cryptography;
using System.IO;
using SyncList.ValueObject;

namespace SyncList.Scanner
{


    /// <summary>
    /// Calculateur de HAsh de fichier
    /// </summary>
    class HashComputer
    {


        /// <summary>
        /// Calculer le Hash d'un fichier
        /// </summary>
        /// <param name="fileName">Chemin et nom du fichier</param>
        public FileDef Compute(string folder, string nameOnly, long minSize)
        {
            FileDef def = null;
            string result = "";
            string fileName = folder + @"\" + nameOnly;
            long length = 0;

            using (HashAlgorithm hashAlg = new SHA1Managed())
            {
                using (Stream file = new FileStream(fileName, FileMode.Open, FileAccess.Read))
                {
                    length = file.Length;
                    if (length >= minSize)
                    {

                        byte[] hash = hashAlg.ComputeHash(file);

                        // Display the hash code of the file to the console.
                        result = BitConverter.ToString(hash);
                        //Console.WriteLine(BitConverter.ToString(hash));

                        def = new FileDef();
                        def.Hash = result;
                        def.LocalFolder = folder;
                        def.Name = nameOnly;
                        def.Size = length;
                        def.LocalFolder = folder;
                    }
                    
                }
            }

         
            return (def);
        }

    }
}
